﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.HVAC;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYTestConn : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				UIApplication application = commandData.Application;
				Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
				Document document = application.ActiveUIDocument.Document;
				Selection selection = commandData.Application.ActiveUIDocument.Selection;
				HYPipeConnect hypipeConnect = new HYPipeConnect();
				Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, "请选择入口管……");
				Pipe item = document.GetElement(reference) as Pipe;
				reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, "请选择出口管……");
				Pipe item2 = document.GetElement(reference) as Pipe;
				reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, "请选择支管1……");
				Pipe item3 = document.GetElement(reference) as Pipe;
				reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, "请选择支管2……");
				Pipe item4 = document.GetElement(reference) as Pipe;
				List<Pipe> list = new List<Pipe>();
				list.Add(item);
				list.Add(item2);
				list.Add(item3);
				list.Add(item4);
				FamilySymbol familySymbolByName = this.GetFamilySymbolByName("立体顺水四通", document);
				transaction = new Transaction(document, "四通连接");
				transaction.Start();
				hypipeConnect.NewDownCrossFitting(list, familySymbolByName);
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		private FamilySymbol GetFamilySymbolByName(string familyName, Autodesk.Revit.DB.Document doc)
		{
			FamilySymbol result = null;
			List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(FamilySymbol), doc);
			if (elementByClass.Count <= 0)
			{
				return null;
			}
			foreach (Element element in elementByClass)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				if (familySymbol != null && familySymbol.Family.Name == "HY_" + familyName)
				{
					result = familySymbol;
					break;
				}
			}
			return result;
		}
	}
}
